`timescale 1ns/1ps
//////////////////////////////////////////////////////////////////////////////////
// Company:         Beijing ICfusion technology Co.Ltd
// Website:         http://www.icfusion.com/
// Create Date:     04/20/2020
//
// Design Name:     zs
// Target Devices:  PRX100T
// Tool Versions:   Vivado18.2
//
// Module Name:     pb_filter
// Project Name:    
// Description:     
// 
// Dependencies: 
// Revision:
// Additional Comments:
// 
//////////////////////////////////////////////////////////////////////////////////
module pb_filter
(
	input	SYS_CLK,
	input	SYS_RST_N,
	input	MS_P,
	input	KEYIN,
	output	KEYOUT
);
     
reg	keyin_r;
reg keyin_rr;
reg keyout_r;

//push_button vibrating elemination
reg	[1:0]	ve_key_st;
reg	[3:0]	ve_key_count;
			
always @ (posedge SYS_CLK)
	begin
		keyin_r <= KEYIN;
		keyin_rr <= keyin_r;
	end
	
      
always @ (posedge SYS_CLK)
	if (~SYS_RST_N) 
	begin
		keyout_r <= 1'b0;
		ve_key_count <= 0;
		ve_key_st <= 0;
	end
	else 
	case (ve_key_st)
		0	:	
		begin
 			keyout_r <= 1'b0;
 			ve_key_count <= 0;
 			if (~keyin_rr)
 				ve_key_st <= 1;
		end
		1	:
		begin
			if (keyin_rr)
				ve_key_st <= 0;
			else 
			begin
				if (ve_key_count == 10) 
					ve_key_st  <=2;
				else if (MS_P)
					ve_key_count <= ve_key_count + 1;
			end    
		end  
		2	:
		begin
			ve_key_count <= 0;
			if (keyin_rr)
				ve_key_st <= 3;
		end  
		3	:
		begin
			if (~keyin_rr)
				ve_key_st <= 2;
			else 
			begin
				if (ve_key_count == 10) 
				begin
					ve_key_st <= 0;
					keyout_r <= 1'b1;
				end
				else if (MS_P)
					ve_key_count <= ve_key_count + 1;
			end    
		end  
		default	:
			ve_key_st <= 0;
	endcase

assign	KEYOUT = keyout_r; 

endmodule

